Imports System.Data
Imports System.Data.SqlClient

Public Class frmConsultaApartados
   Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

   Public Sub New()
      MyBase.New()

      'This call is required by the Windows Form Designer.
      InitializeComponent()

      'Add any initialization after the InitializeComponent() call

   End Sub

   'Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
      If disposing Then
         If Not (components Is Nothing) Then
            components.Dispose()
         End If
      End If
      MyBase.Dispose(disposing)
   End Sub

   'Required by the Windows Form Designer
   Private components As System.ComponentModel.IContainer

   'NOTE: The following procedure is required by the Windows Form Designer
   'It can be modified using the Windows Form Designer.  
   'Do not modify it using the code editor.
   Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
   Friend WithEvents dgExistencias As System.Windows.Forms.DataGrid
   Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
   Friend WithEvents editDescripcion As System.Windows.Forms.TextBox
   Friend WithEvents Label1 As System.Windows.Forms.Label
   Friend WithEvents editProducto As System.Windows.Forms.TextBox
   Friend WithEvents Label2 As System.Windows.Forms.Label
   Friend WithEvents Label3 As System.Windows.Forms.Label
   Friend WithEvents Label4 As System.Windows.Forms.Label
   Friend WithEvents Label5 As System.Windows.Forms.Label
   Friend WithEvents Label6 As System.Windows.Forms.Label
   Friend WithEvents Label8 As System.Windows.Forms.Label
   Friend WithEvents editEmbarcada As System.Windows.Forms.TextBox
   Friend WithEvents editDisponibilidad As System.Windows.Forms.TextBox
   Friend WithEvents editRequerida As System.Windows.Forms.TextBox
   Friend WithEvents editApartados As System.Windows.Forms.TextBox
   Friend WithEvents editExistencia As System.Windows.Forms.TextBox
   Friend WithEvents editDescSucursal As System.Windows.Forms.TextBox
   Friend WithEvents editSucursal As System.Windows.Forms.TextBox
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
      Me.GroupBox2 = New System.Windows.Forms.GroupBox
      Me.dgExistencias = New System.Windows.Forms.DataGrid
      Me.GroupBox1 = New System.Windows.Forms.GroupBox
      Me.Label8 = New System.Windows.Forms.Label
      Me.editEmbarcada = New System.Windows.Forms.TextBox
      Me.Label5 = New System.Windows.Forms.Label
      Me.editDisponibilidad = New System.Windows.Forms.TextBox
      Me.Label6 = New System.Windows.Forms.Label
      Me.editRequerida = New System.Windows.Forms.TextBox
      Me.Label3 = New System.Windows.Forms.Label
      Me.editApartados = New System.Windows.Forms.TextBox
      Me.Label4 = New System.Windows.Forms.Label
      Me.editExistencia = New System.Windows.Forms.TextBox
      Me.editDescSucursal = New System.Windows.Forms.TextBox
      Me.Label2 = New System.Windows.Forms.Label
      Me.editSucursal = New System.Windows.Forms.TextBox
      Me.editDescripcion = New System.Windows.Forms.TextBox
      Me.Label1 = New System.Windows.Forms.Label
      Me.editProducto = New System.Windows.Forms.TextBox
      Me.GroupBox2.SuspendLayout()
      CType(Me.dgExistencias, System.ComponentModel.ISupportInitialize).BeginInit()
      Me.GroupBox1.SuspendLayout()
      Me.SuspendLayout()
      '
      'GroupBox2
      '
      Me.GroupBox2.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                  Or System.Windows.Forms.AnchorStyles.Left) _
                  Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
      Me.GroupBox2.Controls.Add(Me.dgExistencias)
      Me.GroupBox2.Location = New System.Drawing.Point(8, 136)
      Me.GroupBox2.Name = "GroupBox2"
      Me.GroupBox2.Size = New System.Drawing.Size(616, 312)
      Me.GroupBox2.TabIndex = 3
      Me.GroupBox2.TabStop = False
      '
      'dgExistencias
      '
      Me.dgExistencias.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                  Or System.Windows.Forms.AnchorStyles.Left) _
                  Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
      Me.dgExistencias.CaptionVisible = False
      Me.dgExistencias.DataMember = ""
      Me.dgExistencias.HeaderForeColor = System.Drawing.SystemColors.ControlText
      Me.dgExistencias.Location = New System.Drawing.Point(8, 16)
      Me.dgExistencias.Name = "dgExistencias"
      Me.dgExistencias.ReadOnly = True
      Me.dgExistencias.Size = New System.Drawing.Size(600, 288)
      Me.dgExistencias.TabIndex = 0
      Me.dgExistencias.TabStop = False
      '
      'GroupBox1
      '
      Me.GroupBox1.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                  Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
      Me.GroupBox1.Controls.Add(Me.Label8)
      Me.GroupBox1.Controls.Add(Me.editEmbarcada)
      Me.GroupBox1.Controls.Add(Me.Label5)
      Me.GroupBox1.Controls.Add(Me.editDisponibilidad)
      Me.GroupBox1.Controls.Add(Me.Label6)
      Me.GroupBox1.Controls.Add(Me.editRequerida)
      Me.GroupBox1.Controls.Add(Me.Label3)
      Me.GroupBox1.Controls.Add(Me.editApartados)
      Me.GroupBox1.Controls.Add(Me.Label4)
      Me.GroupBox1.Controls.Add(Me.editExistencia)
      Me.GroupBox1.Controls.Add(Me.editDescSucursal)
      Me.GroupBox1.Controls.Add(Me.Label2)
      Me.GroupBox1.Controls.Add(Me.editSucursal)
      Me.GroupBox1.Controls.Add(Me.editDescripcion)
      Me.GroupBox1.Controls.Add(Me.Label1)
      Me.GroupBox1.Controls.Add(Me.editProducto)
      Me.GroupBox1.Location = New System.Drawing.Point(8, 8)
      Me.GroupBox1.Name = "GroupBox1"
      Me.GroupBox1.Size = New System.Drawing.Size(616, 120)
      Me.GroupBox1.TabIndex = 2
      Me.GroupBox1.TabStop = False
      '
      'Label8
      '
      Me.Label8.Location = New System.Drawing.Point(416, 72)
      Me.Label8.Name = "Label8"
      Me.Label8.Size = New System.Drawing.Size(64, 16)
      Me.Label8.TabIndex = 15
      Me.Label8.Text = "Embarcada"
      '
      'editEmbarcada
      '
      Me.editEmbarcada.Location = New System.Drawing.Point(480, 64)
      Me.editEmbarcada.Name = "editEmbarcada"
      Me.editEmbarcada.ReadOnly = True
      Me.editEmbarcada.Size = New System.Drawing.Size(128, 20)
      Me.editEmbarcada.TabIndex = 14
      Me.editEmbarcada.TabStop = False
      Me.editEmbarcada.Text = ""
      '
      'Label5
      '
      Me.Label5.Location = New System.Drawing.Point(200, 96)
      Me.Label5.Name = "Label5"
      Me.Label5.Size = New System.Drawing.Size(80, 16)
      Me.Label5.TabIndex = 13
      Me.Label5.Text = "Disponibilidad"
      '
      'editDisponibilidad
      '
      Me.editDisponibilidad.Location = New System.Drawing.Point(280, 88)
      Me.editDisponibilidad.Name = "editDisponibilidad"
      Me.editDisponibilidad.ReadOnly = True
      Me.editDisponibilidad.Size = New System.Drawing.Size(128, 20)
      Me.editDisponibilidad.TabIndex = 12
      Me.editDisponibilidad.TabStop = False
      Me.editDisponibilidad.Text = ""
      '
      'Label6
      '
      Me.Label6.Location = New System.Drawing.Point(200, 72)
      Me.Label6.Name = "Label6"
      Me.Label6.Size = New System.Drawing.Size(72, 16)
      Me.Label6.TabIndex = 11
      Me.Label6.Text = "Requerida"
      '
      'editRequerida
      '
      Me.editRequerida.Location = New System.Drawing.Point(280, 64)
      Me.editRequerida.Name = "editRequerida"
      Me.editRequerida.ReadOnly = True
      Me.editRequerida.Size = New System.Drawing.Size(128, 20)
      Me.editRequerida.TabIndex = 10
      Me.editRequerida.TabStop = False
      Me.editRequerida.Text = ""
      '
      'Label3
      '
      Me.Label3.Location = New System.Drawing.Point(8, 96)
      Me.Label3.Name = "Label3"
      Me.Label3.Size = New System.Drawing.Size(56, 16)
      Me.Label3.TabIndex = 9
      Me.Label3.Text = "Apartados"
      '
      'editApartados
      '
      Me.editApartados.Location = New System.Drawing.Point(64, 88)
      Me.editApartados.Name = "editApartados"
      Me.editApartados.ReadOnly = True
      Me.editApartados.Size = New System.Drawing.Size(128, 20)
      Me.editApartados.TabIndex = 8
      Me.editApartados.TabStop = False
      Me.editApartados.Text = ""
      '
      'Label4
      '
      Me.Label4.Location = New System.Drawing.Point(8, 72)
      Me.Label4.Name = "Label4"
      Me.Label4.Size = New System.Drawing.Size(56, 16)
      Me.Label4.TabIndex = 7
      Me.Label4.Text = "Existencia"
      '
      'editExistencia
      '
      Me.editExistencia.Location = New System.Drawing.Point(64, 64)
      Me.editExistencia.Name = "editExistencia"
      Me.editExistencia.ReadOnly = True
      Me.editExistencia.Size = New System.Drawing.Size(128, 20)
      Me.editExistencia.TabIndex = 6
      Me.editExistencia.TabStop = False
      Me.editExistencia.Text = ""
      '
      'editDescSucursal
      '
      Me.editDescSucursal.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                  Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
      Me.editDescSucursal.Location = New System.Drawing.Point(200, 40)
      Me.editDescSucursal.Name = "editDescSucursal"
      Me.editDescSucursal.ReadOnly = True
      Me.editDescSucursal.Size = New System.Drawing.Size(408, 20)
      Me.editDescSucursal.TabIndex = 5
      Me.editDescSucursal.TabStop = False
      Me.editDescSucursal.Text = ""
      '
      'Label2
      '
      Me.Label2.Location = New System.Drawing.Point(8, 48)
      Me.Label2.Name = "Label2"
      Me.Label2.Size = New System.Drawing.Size(56, 16)
      Me.Label2.TabIndex = 4
      Me.Label2.Text = "Sucursal"
      '
      'editSucursal
      '
      Me.editSucursal.Location = New System.Drawing.Point(64, 40)
      Me.editSucursal.Name = "editSucursal"
      Me.editSucursal.Size = New System.Drawing.Size(128, 20)
      Me.editSucursal.TabIndex = 3
      Me.editSucursal.Text = ""
      '
      'editDescripcion
      '
      Me.editDescripcion.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                  Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
      Me.editDescripcion.Location = New System.Drawing.Point(200, 16)
      Me.editDescripcion.Name = "editDescripcion"
      Me.editDescripcion.ReadOnly = True
      Me.editDescripcion.Size = New System.Drawing.Size(408, 20)
      Me.editDescripcion.TabIndex = 2
      Me.editDescripcion.TabStop = False
      Me.editDescripcion.Text = ""
      '
      'Label1
      '
      Me.Label1.Location = New System.Drawing.Point(8, 24)
      Me.Label1.Name = "Label1"
      Me.Label1.Size = New System.Drawing.Size(56, 16)
      Me.Label1.TabIndex = 1
      Me.Label1.Text = "Producto"
      '
      'editProducto
      '
      Me.editProducto.Location = New System.Drawing.Point(64, 16)
      Me.editProducto.Name = "editProducto"
      Me.editProducto.Size = New System.Drawing.Size(128, 20)
      Me.editProducto.TabIndex = 0
      Me.editProducto.Text = ""
      '
      'frmConsultaApartados
      '
      Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
      Me.ClientSize = New System.Drawing.Size(632, 453)
      Me.Controls.Add(Me.GroupBox2)
      Me.Controls.Add(Me.GroupBox1)
      Me.Name = "frmConsultaApartados"
      Me.Text = " Consulta de Apartados"
      Me.GroupBox2.ResumeLayout(False)
      CType(Me.dgExistencias, System.ComponentModel.ISupportInitialize).EndInit()
      Me.GroupBox1.ResumeLayout(False)
      Me.ResumeLayout(False)

   End Sub

#End Region

   Private mdsApartados As DataSet

   Private Sub editProducto_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles editProducto.KeyUp

      If e.KeyCode = Keys.F3 Then

         Dim strResult As String
         Dim fBusqueda As New frmBusqueda

         fBusqueda.Tabla = "Inventory"
         fBusqueda.Campos = "InvtID, Descr"

         fBusqueda.ShowDialog()
         strResult = fBusqueda.Valor

         If strResult <> "" Then
            editProducto.Text = strResult
            editSucursal.Focus()
         End If

      End If

   End Sub

   Private Sub editSucursal_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles editSucursal.KeyUp

      If e.KeyCode = Keys.F3 Then

         Dim strResult As String
         Dim fBusqueda As New frmBusqueda

         fBusqueda.Tabla = "pvSucursales"
         fBusqueda.Campos = "Sucursal, Descripcion"
         fBusqueda.Where = "Where Regional = '" & gstrRegional & "'"
         fBusqueda.ShowDialog()
         strResult = fBusqueda.Valor

         If strResult <> "" Then
            editSucursal.Text = strResult
            editDescSucursal.Focus()
         End If

      End If

   End Sub

   Private Sub editProducto_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles editProducto.Validating

      Try

         Cursor.Current = Cursors.WaitCursor
         gsbpTexto.Text = "Buscando..."

         If editProducto.Text.Trim <> "" Then
            editDescripcion.Text = BuscarStrDato("Inventory", "Descr", "InvtID = '" & editProducto.Text.Trim & "'")
         End If

         If editProducto.Text.Trim <> "" And editSucursal.Text.Trim <> "" Then
            BuscarInformacion()
         End If

      Catch ex As Exception

      Finally

         Cursor.Current = Cursors.Default
         gsbpTexto.Text = "Listo."

      End Try

   End Sub

   Private Sub editSucursal_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles editSucursal.Validating

      Try

         Cursor.Current = Cursors.WaitCursor
         gsbpTexto.Text = "Buscando..."

         If editSucursal.Text.Trim <> "" Then
            editDescSucursal.Text = BuscarStrDato("pvSucursales", "Descripcion", "Regional = '" & gstrRegional & "' And Sucursal = '" & editSucursal.Text.Trim & "'")
         End If

         If editProducto.Text.Trim <> "" And editSucursal.Text.Trim <> "" Then
            BuscarInformacion()
         End If

      Catch ex As Exception

      Finally

         Cursor.Current = Cursors.Default
         gsbpTexto.Text = "Listo."

      End Try

   End Sub

   Private Sub BuscarInformacion()
      Dim cnnConn As New SqlConnection
      Dim strSqlQry As String

      Try

         cnnConn.ConnectionString = gstrConnection
         cnnConn.Open()

         strSqlQry = "Select Existencia, Requerida, Embarcada, Apartada, " & _
                     "      (Existencia + Requerida + Embarcada - Apartada) As Disponibilidad " & _
                     "From pvvwExistencias " & _
                     "Where Empresa = '" & gstrEmpresa & "'" & _
                     "  And Regional = '" & gstrRegional & "'" & _
                     "  And Sucursal = '" & editSucursal.Text.Trim & "'" & _
                     "  And Producto = '" & editProducto.Text.Trim & "'"

         Dim cmdBuscar As New SqlCommand
         cmdBuscar.CommandText = strSqlQry
         cmdBuscar.Connection = cnnConn

         Dim rdExistencias As SqlDataReader

         rdExistencias = cmdBuscar.ExecuteReader

         Dim dblExistencia As Double
         Dim intRequerida As Integer
         Dim intEmbarcada As Integer
         Dim intApartada As Integer
         Dim dblDisponibilidad As Double

         If rdExistencias.Read() Then

            Try
               dblExistencia = rdExistencias.GetDouble(0)
            Catch ex As Exception
               dblExistencia = 0
            End Try

            Try
               intRequerida = rdExistencias.GetInt32(1)
            Catch ex As Exception
               intRequerida = 0
            End Try

            Try
               intEmbarcada = rdExistencias.GetInt32(2)
            Catch ex As Exception
               intEmbarcada = 0
            End Try

            Try
               intApartada = rdExistencias.GetInt32(3)
            Catch ex As Exception
               intApartada = 0
            End Try

            Try
               dblDisponibilidad = rdExistencias.GetDouble(4)
            Catch ex As Exception
               dblDisponibilidad = 0
            End Try

         End If

         rdExistencias.Close()

         editExistencia.Text = dblExistencia
         editRequerida.Text = intRequerida
         editEmbarcada.Text = intEmbarcada
         editApartados.Text = intApartada
         editDisponibilidad.Text = dblDisponibilidad

         ' -------------------------------------------------------------------------
         Dim scmdSelect As New SqlCommand
         scmdSelect.CommandText = "Select pvApartadosHdr.Folio, CteNombre As Nombre, CteDir1 As Direccion1, CteDir2 As Direccion2, " & _
                                  "       CteTelCasa As TelCasa, CteTelOfic As TelOficina, Fecha As FechaApartado, " & _
                                  "       DateAdd(d, Cast(FormApart As Int), Fecha) As FechaEntrega " & _
                                  "From pvApartadosHdr Inner Join pvApartadosDet On " & _
                                  "     pvApartadosHdr.Empresa = pvApartadosDet.Empresa And " & _
                                  "     pvApartadosHdr.Regional = pvApartadosDet.Regional And " & _
                                  "     pvApartadosHdr.Sucursal = pvApartadosDet.Sucursal And " & _
                                  "     pvApartadosHdr.Caja = pvApartadosDet.Caja And " & _
                                  "     pvApartadosHdr.Cajero = pvApartadosDet.Cajero And " & _
                                  "     pvApartadosHdr.Folio = pvApartadosDet.Folio " & _
                                  "Where pvApartadosHdr.Empresa = @Empresa " & _
                                  "  And pvApartadosHdr.Regional = @Regional " & _
                                  "  And pvApartadosHdr.Sucursal = @Sucursal " & _
                                  "  And pvApartadosDet.Producto = @Producto "

         scmdSelect.CommandType = CommandType.Text
         scmdSelect.Connection = cnnConn

         scmdSelect.Parameters.Add("@Empresa", gstrEmpresa)
         scmdSelect.Parameters.Add("@Regional", gstrRegional)
         scmdSelect.Parameters.Add("@Sucursal", editSucursal.Text.Trim)
         scmdSelect.Parameters.Add("@Producto", editProducto.Text.Trim)

         If mdsApartados.Tables.Count() > 0 Then
            mdsApartados.Tables("Apartados").Clear()
         End If

         Dim daApartados As New SqlDataAdapter
         daApartados.SelectCommand = scmdSelect

         daApartados.Fill(mdsApartados, "Apartados")

      Catch ex As SqlException
         MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
      Finally
         If cnnConn.State = ConnectionState.Open Then
            cnnConn.Close()
         End If
      End Try

   End Sub

   Private Sub frmConsultaApartados_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

      mdsApartados = New DataSet

      BuscarInformacion()

      dgExistencias.DataSource = mdsApartados.Tables("Apartados")

   End Sub

End Class
